#include "request.h"

#include <QString>
#include <QJsonObject>
#include <QJsonDocument>
#include <QtDebug>
Request * Request::ptrRequest = nullptr;
Request::Request(QObject *parent)
    : QObject{parent}
{
    init();
}

void Request::init()
{
    accessManager = new QNetworkAccessManager(this);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");//构建json头
    //测试post
    //reply = accessManager->post(request, QByteArray("{\"password\": \"123456\", \"phone\": \"15683141454\"}"));
}

void Request::LoginByPassword(QString userName, QString pwd)
{
    QJsonObject json;
    json.insert("phone",userName);
    json.insert("password",pwd);

    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    request.setUrl(QUrl("http://81.70.102.35:8082/user/auth/loginByPassword"));//设置url
    reply = accessManager->post(request, postData);//发送POST请求
}





void Request::finishedSlot(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
        {
            QByteArray bytes = reply->readAll();
            //qDebug() << bytes;
            QJsonParseError jsonError;
            QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);  // 转化为 JSON 文档
            QJsonObject object = doucment.object();
            //qDebug()<<object;
            if (object.contains("code"))
            {
                QJsonValue value = object.value("code");  // 获取指定 key 对应的 value
                this->result.code=value.toInt();
                //qDebug()<<value.toInt();
                //qDebug()<<this->result.code;
            }
            if(object.contains("data"))
            {
                if(reply->url().toString()=="http://81.70.102.35:8082/user/auth/loginByPassword")
                {
                    QJsonValue value = object.value("data");
                    if(value.isObject())
                    {
                        QJsonObject obj = value.toObject();
                        if(obj.contains("token"))
                        {
                            QJsonValue value=obj.value("token");
                            this->result.token=value.toString();
                        }
                    }
                    //qDebug()<<result.data;
                    //qDebug()<<this->result.data;
                }


            }
            if(object.contains("message"))
            {
                QJsonValue value = object.value("message");
                this->result.message=value.toString();
                //qDebug()<<"message"+value.toString();
                //qDebug()<<this->result.message;
            }

            //emit sendSuccess(reply->url().toString());//发送信号
            if(reply->url().toString()=="http://81.70.102.35:8082/user/auth/loginByPassword")
            {
                emit sendLoginByPasswordSuccess();
            }
            if(reply->url().toString()=="http://81.70.102.35:8082/user/dish/recommend")
            {
                //qDebug()<<"菜谱推荐";
                //emit sendDishRecommendSuccess();
            }
            //showResult();
        }
        else
        {
            qDebug() << "finishedSlot errors here";
            qDebug( "found error .... code: %d\n", (int)reply->error());
            qDebug(qPrintable(reply->errorString()));
        }
        reply->deleteLater();
}
